package rsj.api.product;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.user.BaseLink;
import rsj.api.user.entity.UserAccount;
import rsj.domain.ProdUpListQuery;
import rsj.service.ProdUpService;
import rsj.service.ProductService;
import rsj.service.user.IUserService;

@RestController
public class GetProductInfo extends BaseLink {

    @Autowired
    private ProductService productService;
    private static final ProdUpService proUpService = ProdUpService.getInstance();

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "rsj_prod_findId")
    public Box executeLink(Box in) {
        String prodId = getAndCheckProdId(in);
        String userId = in.getMapNode("$user").getValue("User_Id");
        UserAccount userInfo = userService.getUserInfo(userId);
        MapNode prodInfo = productService.getProductInfoById(prodId, userInfo.getUserOrgId());
        prodInfo.putNode("Up_Pro", getUpProdList(prodId).toJson());

        return BoxUtil.successBox(in, "获取产品信息！").putMapNode("Prod_Info", prodInfo);
    }

    private TableNode getUpProdList(String prodId) {
        ProdUpListQuery query = new ProdUpListQuery();
        query.setProd_Id(prodId);
        query.setPageSize(10000);

        return proUpService.getUpPro(query);
    }

    private String getAndCheckProdId(Box in) {
        MapNode data = in.getMapNode("data");
        Assert.isNotNull(data, "没有收到要新增的信息！");
        String prodId = data.getValueDef("Prod_Id", null);
        Assert.isNotBlank(prodId, "产品ID 不能为空");
        return prodId;
    }

}
